package com.nobody.facepk.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.nobody.facepk.model.Friends;
import com.nobody.facepk.model.Match;
import com.nobody.facepk.model.Picture;
import com.nobody.facepk.model.PictureData;
import com.nobody.facepk.model.PictureDataType;
import com.nobody.facepk.model.User;
import com.nobody.facepk.pojo.PictureDataHelper;
import com.nobody.facepk.pojo.SessionUser;
import com.nobody.facepk.pojo.json.Scoring;
import com.nobody.facepk.service.CommentService;
import com.nobody.facepk.service.PictureService;
import com.nobody.facepk.service.ServiceRegistry;
import com.nobody.facepk.service.UserService;
import com.nobody.facepk.utils.UserUtils;
import com.nobody.facepk.view.MatchView;
import com.nobody.facepk.view.ScoringResultView;
import com.trg.dao.hibernate.GeneralDAO;

@Controller
public class PictureController {
	private PictureService pictureService;
	private GeneralDAO generalDao;
	private CommentService commentService;
	private UserService userService;

	@RequestMapping("/piclist.do")
	public ModelAndView listPic(HttpServletRequest request) {
		Long userId = UserUtils.getUserId();
		List<Picture> pictures = ServiceRegistry.getPictureService().getPictures(userId);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("piclist");
		mav.addObject("pictures",pictures);
		return mav;
	}
	
	@RequestMapping("/login.do")
	public String login(HttpServletRequest request, Long userId) {
		SessionUser sessionUser = new SessionUser();
		sessionUser.setUserId(userId);
		request.getSession().setAttribute("SESSION_USER",sessionUser);
		return "home";
	}
	
//	@RequestMapping("/vote.do")
//	public String score(Long pictureId, int score) {
//		Long userId = UserUtils.getUserId();
//		this.pictureService.score(userId, pictureId, score);
//		return "ok";
//	}
	
	@RequestMapping("/gradeList.do")
	public ModelAndView gradeList(Long pictureId) {
		Long userId = UserUtils.getUserId();
		//我的照片集合
		List<Picture> pictures = ServiceRegistry.getPictureService().getMyInitPictures(userId);
		ModelAndView mav = null;
		
		
		//好友的照片集合
		List<Picture> friendPics  = new ArrayList<Picture>();
		Friends friends = this.userService.getMyFriends(userId);
		Set<Long> friendIds = friends.getFriendIds();
		for(long friendId : friendIds){
			List<Picture> friendPictures = ServiceRegistry.getPictureService().getMyInitPictures(friendId);
			if(friendPictures != null){
				if(friendPictures.size() > 0 ){
					friendPics.addAll(friendPictures);
				}
			}
			
		}
		mav = new ModelAndView("gradeList");
		mav.addObject("friendPictures",friendPics);
		mav.addObject("myPictures",pictures);
		return mav;
	}
	
	@RequestMapping("/pkList.do")
	public ModelAndView pkList() {
		Long userId = UserUtils.getUserId();
		//我的可以PK的照片
		List<Picture> freePictures = ServiceRegistry.getPictureService().getFreePicturesById(userId);
		//好友的照片集合
		List<Picture> friendPics  = new ArrayList<Picture>();
		Friends friends = this.userService.getMyFriends(userId);
		Set<Long> friendIds = friends.getFriendIds();
		for(long friendId : friendIds){
			List<Picture> friendPictures = ServiceRegistry.getPictureService().getFreePicturesById(friendId);
			if(friendPictures != null){
				if(friendPictures.size() > 0 ){
					friendPics.addAll(friendPictures);
				}
			}
		}
		ModelAndView mav = null;
		mav = new ModelAndView("pkList");
		mav.addObject("freePictures",freePictures);
		mav.addObject("friendPics",friendPics);
		return mav;
	}
	
	@RequestMapping("/grade.do")
	public String grade(Long pictureId) {
		Long userId = UserUtils.getUserId();
		this.pictureService.grade(userId, pictureId);
		return "ok";
	}
	
	@RequestMapping("/scorePicture.do")
	public ModelAndView scorePicture(Long id) {
		Picture p = this.generalDao.find(Picture.class, id);
		List<User> users = this.pictureService.getScoredUserByPicId(p.getId());
		ModelAndView mav = null;
		mav = new ModelAndView("score");
		mav.addObject("picture", p);
		mav.addObject("users",users);
		return mav;
	}
	
	@RequestMapping("/scoringPic.do")
	public ModelAndView scoringPic(Long pid,int score) {
		Picture p = this.generalDao.find(Picture.class, pid);
		this.pictureService.scoring(p, score);
		PictureData pd = PictureDataHelper.getPictureData(pid, PictureDataType.SCORINGS);
		List<Scoring> list = pd.getDataAsList(Scoring.LIST_TYPE);
		ModelAndView mav = null;
		
		List<ScoringResultView> ret  = new ArrayList<ScoringResultView>();
		ScoringResultView srv = null;
		for(int i = 0 ; i < list.size() ; i ++){
			srv = new ScoringResultView();
			User u  = this.generalDao.find(User.class,list.get(i).getUserId());
			if(u != null){
				srv.setUserId(u.getId());
				srv.setScore(list.get(i).getScore());
				srv.setName(u.getName());
				ret.add(srv);
			}
		}
		mav = new ModelAndView("scoreresult");
		mav.addObject("ret", ret);
		mav.addObject("picture", p);
//		ModelAndView mav = null;
//		mav = new ModelAndView("score");
//		mav.addObject("picture", p);
		return mav;
	}
	
	@RequestMapping("/choosePic.do")
	public ModelAndView choosePic(Long picId){
		Long userId = UserUtils.getUserId();
		Picture p = this.generalDao.find(Picture.class, picId);
		List<Picture> pictures = this.pictureService.choosePic(userId,p);
		System.out.println("Size = "+pictures.size());
		ModelAndView mav = null;
		mav = new ModelAndView("choosepicture");
		mav.addObject("pictures",pictures);
		mav.addObject("picId",picId);
		return mav;
	}
	
	@RequestMapping("/comment.do")
	public ModelAndView comment( Long picId ) {
		Picture p = this.generalDao.find(Picture.class, picId);
		ModelAndView mav = null;
		mav = new ModelAndView("comment");
		
		List list = this.commentService.getCommentByPicid(picId);
		mav.addObject("comment",list);
		mav.addObject("picture", p);
		return mav;
	}
	@RequestMapping("/match.do")
	public ModelAndView match(Long picId,Long targetPicId){
		Picture pic = this.generalDao.find(Picture.class, picId);
		Picture targetPic =this.generalDao.find(Picture.class, targetPicId);
		ModelAndView mav = null;
		boolean isSuccess = this.pictureService.match(pic, targetPic);
		
		if(isSuccess){
			mav = new ModelAndView("ok");
			return mav;
		}else{
			mav = new ModelAndView("error");
			return mav;
		}
		
	}
	
	
	@RequestMapping("/commentOnPic.do")
	public String commentOnPic(HttpServletRequest request ,String content , Long picId ,Long refereId) {
		SessionUser session = (SessionUser)request.getSession().getAttribute("SESSION_USER");
		long userId = session.getUserId();
		this.commentService.insertComment(picId, refereId, content, userId);
		return "ok";
	}
	@RequestMapping("/poll.do")
	public String poll(Long matchid,Long id){
		Long userId = UserUtils.getUserId();
		this.pictureService.poll(userId, matchid, id);
		return "ok";
	}
	
	@RequestMapping("/pollInfo.do")
	public ModelAndView pollInfo(Long matchid,Long id){
		Match m = this.generalDao.find(Match.class, matchid);
		MatchView mv = new MatchView();
		Picture hostPic = ServiceRegistry.getPictureService().getPictureByid(m.getHostId());
		Picture guestPic = ServiceRegistry.getPictureService().getPictureByid(m.getGuestId());
		mv.setMatchId(matchid);
		mv.setHostId(m.getHostId());
		mv.setGuestId(m.getGuestId());
		mv.setHostPath(hostPic.getPath());
		mv.setGuestPath(guestPic.getPath());
		mv.setHostUserid(hostPic.getUserId());
		mv.setGuestUserid(guestPic.getUserId());
		ModelAndView mav = null;
		mav = new ModelAndView("matchinfo");
		mav.addObject("mv", mv);
		System.out.println("matchinfo");
		return mav;
	}
	
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@Autowired
	public void setGeneralDao(GeneralDAO generalDao) {
		this.generalDao = generalDao;
	}
	@Autowired
	public void setPictureService(PictureService pictureService) {
		this.pictureService = pictureService;
	}
	@Autowired
	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
}
